package com.blockmeta.bbs.minemodule.fragment;

import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.minemodule.R;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

/**
 * @author AirThor
 * @date 2019/12/10.
 */
public abstract class BaseMyActiveListFragment<T extends BaseQuickAdapter> extends BaseMyActiveRefreshFragment implements BaseQuickAdapter.RequestLoadMoreListener {
    public T mActiveListAdapter;
    protected int mPageNo = 1;
    protected RecyclerView mRcvActiveList;

    @Override
    protected View setContentViewIn(LayoutInflater inflater, FrameLayout contentArea) {
        mRcvActiveList = (RecyclerView) inflater.inflate(R.layout.fragment_my_active_list,contentArea,false);
        initActiveList(mRcvActiveList);
        return mRcvActiveList;
    }

    private void initActiveList(RecyclerView rcvActiveList) {
        rcvActiveList.setLayoutManager(new LinearLayoutManager(mContext));
        mActiveListAdapter = initAdapter();
        rcvActiveList.setAdapter(mActiveListAdapter);
        if (mActiveListAdapter!=null) {
            mActiveListAdapter.setOnLoadMoreListener(this, rcvActiveList);
        }
    }

    /**
     * 已无更多数据
     */
    protected void loadMorEnd(){
        if (mActiveListAdapter!=null&&mActiveListAdapter.isLoading()) {
            mActiveListAdapter.loadMoreEnd();
        }
    }

    /**
     * 加载更多已完成
     */
    protected void loadMoreComplete(){
        if (mActiveListAdapter!=null&&mActiveListAdapter.isLoading()){
            mActiveListAdapter.loadMoreComplete();
        }
    }
    /**
     * 加载更多失败
     */
    protected void loadMoreFail(){
        if (mActiveListAdapter!=null&&mActiveListAdapter.isLoading()){
            mActiveListAdapter.loadMoreFail();
        }
    }

    /**
     * 子类初始化具体的adapter
     * @return adapter
     */
    protected abstract T initAdapter();

    /**
     * 这个网络请求回调专为此基类的实现类提供，隐藏结束刷新等和业务无关的操作
     */
    protected abstract class SubscriberForActive<T extends HasMoreData> extends CommonSubscriber<T>{

        public SubscriberForActive(Context context) {
            super(context);
        }

        public SubscriberForActive(Context context, boolean showOtherException) {
            super(context, showOtherException);
        }

        @Override
        public void onNext(T t) {
            if (t.hasMoreData()){
                mPageNo++;
                loadMoreComplete();
            }else {
                loadMorEnd();
            }
        }

        @Override
        public void onComplete() {
            super.onComplete();
            finishRefresh();
        }

        @Override
        public void onError(Throwable t) {
            super.onError(t);
            finishRefresh();
            loadMoreFail();
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mPageNo = 1;
    }

    public interface HasMoreData{
        boolean hasMoreData();
    }
}
